package com.me.web;

import Model.Student;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
//import org.springframework.web.servlet.DispatcherServlet;

import com.me.entity.User;
import com.me.service.UserService;
import com.me.service.imp.UserServiceImp;
import java.io.PrintWriter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class LoginController {

    private UserService userService;

    public LoginController() {
    }

    @RequestMapping(value = "/test.aspx", method = RequestMethod.GET)
    public void Test(HttpServletRequest req, HttpServletResponse res) throws Exception {
        res.setContentType("text/html;charset=UTF-8");
        PrintWriter out = res.getWriter();
        try {
            /* TODO output your page here*/
            out.println("test ,in login1");

        } finally {
            out.close();
        }
    }

    @RequestMapping(value = "/checkuser.aspx", method = RequestMethod.GET)
    public void CheckUser(HttpServletRequest req, HttpServletResponse res) throws Exception {
        PrintWriter out = res.getWriter();
        try {
            /* TODO output your page here*/
            if (userService.checkUser("张三", "000000")) {

                out.println("login success");
                System.out.println("CheckUser：login success");
            } else {
                out.println("failure");
                System.out.println("CheckUser：login failure");
            }

        } catch (Exception ex) {
            System.out.println("CheckUser：login Exception:" + ex.getMessage());
            UserServiceImp uc=new UserServiceImp();
             if (uc.checkUser("张三", "000000")) {

                out.println("login success");
                System.out.println("CheckUser：login success");
            } else {
                out.println("failure");
                System.out.println("CheckUser：login failure");
            }
            
        } finally {
            out.close();
        }
    }

    @RequestMapping(value = "/login/test", method = RequestMethod.GET)
    public void Testlogin(HttpServletRequest req, HttpServletResponse res) throws Exception {
        res.setContentType("text/html;charset=UTF-8");
        PrintWriter out = res.getWriter();
        try {
            /* TODO output your page here*/
            out.println("login.test");
        } finally {
            out.close();
        }
    }

    protected ModelAndView handle(HttpServletRequest req,
            HttpServletResponse res, Object command, BindException e)
            throws Exception {
        LoginCommand loginCommand = (LoginCommand) command;

        User user = new User();
        user.setUsername("彭承龙");
        req.getSession().setAttribute("user", user);
        return new ModelAndView("main");
//		LoginCommand loginCommand = (LoginCommand)command;
//		System.out.print("username="+loginCommand.getUsername());
//		if(userService.checkUser(loginCommand.getUsername(),loginCommand.getPassword())){
//			
//			User user = userService.findUserByUserName(loginCommand.getUsername());
//			user.setLastIP(req.getLocalAddr());
//			user.setLastVisit(new Date());
//			userService.loginSuccess(user);
//			req.getSession().setAttribute("user", user);
//			return new ModelAndView("main");
//		}else{
//			//用户不存在�û�������
//			return new ModelAndView("login","error","用户名或密码错误");
//		}

    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
